﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerMauSuatAnDao : IMauSuatAnDao
    {
        public SqlServerMauSuatAnDao()
        {
            SQLUtility.TableName = "MauSuatAn";
        }

        //Trả về đối tượng báo ăn
        private MauSuatAn MakeMauSuatAn(DataRow row)
        {
            Guid _id=Guid.Parse(row["Id"].ToString());
            string _tenMauSuatAn = row["TenMauSuatAn"].ToString();
            int _nhomTuoiId = int.Parse(row["NhomTuoiId"].ToString());
            int _soLuongTre = int.Parse(row["SoLuongTre"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());


         return new MauSuatAn(_id, _tenMauSuatAn, _nhomTuoiId, _soLuongTre, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<MauSuatAn> MakeMauSuatAns(DataTable dt)  
        {
            IList<MauSuatAn> list = new List<MauSuatAn>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeMauSuatAn(row));
            }
            return list;
        }
        private DbParameter[] Take(MauSuatAn MauSuatAn)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@Id", MauSuatAn.Id);
            parameters[1] = new SqlParameter("@TenMauSuatAn", MauSuatAn.TenMauSuatAn);
            parameters[2] = new SqlParameter("@NhomTuoiId", MauSuatAn.NhomTuoiId);
            parameters[3] = new SqlParameter("@SoLuongTre", MauSuatAn.SoLuongTre);
            parameters[4] = new SqlParameter("@SchoolID", MauSuatAn.SchoolID);
            return parameters;
        }
        public IList<Business_Objects.MauSuatAn> GetMauSuatAn()
        {
            SQLUtility.TableName = "MauSuatAn";
           DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeMauSuatAns(dt); 
        }

        public Business_Objects.MauSuatAn GetMauSuatAnByID(string MauSuatAnID)
        {
            SQLUtility.TableName = "MauSuatAn";
          DbParameter[] parameters = new DbParameter[1];
          parameters[0] = new SqlParameter("@Id", MauSuatAnID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeMauSuatAn(row);
            }
        }

        public void InsertMauSuatAn(Business_Objects.MauSuatAn MauSuatAn)
        {
            SQLUtility.TableName = "MauSuatAn";
          int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(MauSuatAn), true);
        }

        public int UpdateMauSuatAn(Business_Objects.MauSuatAn MauSuatAn)
        {
            SQLUtility.TableName = "MauSuatAn";
             try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(MauSuatAn)); }
            catch { return -1; }
        }

        public int DeleteMauSuatAn(Business_Objects.MauSuatAn MauSuatAn)
        {
            SQLUtility.TableName = "MauSuatAn";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(MauSuatAn)); }
            catch { return -1; }
        }
    }
}
